import { MockMethod } from 'vite-plugin-mock'
import path from 'path'
import fs from 'fs'

// 人员信息+项目信息
let peopleinfo = JSON.parse(
  fs.readFileSync(path.join(__dirname, './people.json')) + '',
)

/*材料信息+公司信息+库存信息*/
let materialinfo = JSON.parse(
  fs.readFileSync(path.join(__dirname, './material.json')) + '',
)

// 流程信息
let officeinfo = JSON.parse(
  fs.readFileSync(path.join(__dirname, './office.json')) + '',
)

//邮件信息
let mailinfo = JSON.parse(
  fs.readFileSync(path.join(__dirname, './mail.json')) + '',
)
//计划信息
let planinfo = JSON.parse(
  fs.readFileSync(path.join(__dirname, './plan.json')) + '',
)
//日程信息
let scheduleinfo = JSON.parse(
  fs.readFileSync(path.join(__dirname, './schedule.json')) + '',
)

// fs.writeFileSync(__dirname + '/material.json', JSON.stringify(materialinfo))

export default [
  {
    /* 登录 */
    url: '/admin/login',
    method: 'post',
    response: ({ body }) => {
      const { _value } = body
      // console.log(_value)
      if (_value.adminname === 'GGbond' && _value.password === '123456') {
        return {
          code: 200,
          data: {
            status: 200,
            token: 'aaklsdfjlakdjsflaksdjflakjsdflaksdjflaksdfj',
            message: '登录成功',
          },
          message: '成功',
        }
      }
      return {
        code: 200,
        data: {
          status: 400,
          message: '用户名或密码错误',
        },
        message: '失败',
      }
    },
  },
  {
    /*  角色*/
    url: '/admin/userinfo',
    method: 'post',
    timeout: 2000,
    response: {
      code: 200,
      data: {
        roles: {
          id: 'abcdefg',
          name: '超级管理员',
          avatarUrl:
            'https://img0.baidu.com/it/u=2752729029,3054733954&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1694970000&t=440b22b722c0d06616a0418977cad9a8',
        },
        status: '200',
        userInfo: {
          username: 'GGbond',
          nickname: '猪猪侠',
          phone: '13900887654',
        },
      },
    },
  },
  /********************************************************************************/
  {
    /*获取人员信息*/
    url: '/admin/peopleinfo', // API
    method: 'get',
    response: {
      code: 200, //请求成功返回
      data: peopleinfo,
    },
  },
  {
    /*增加人员信息*/
    url: '/admin/peopleinfo',
    method: 'post',
    response: ({ body }) => {
      // console.log('被添加的主体:', body)

      // 处理添加逻辑
      const has = peopleinfo.some((prod: any) => prod.phone === body.phone)
      if (!has) {
        // 如果手机号没有重复，添加进仓库+写入文件+返回成功的状态
        body['key'] = Math.random()
        peopleinfo.push(body)
        // console.log(body)

        // 写入文件保存
        fs.writeFileSync(__dirname + '/people.json', JSON.stringify(peopleinfo))
        return {
          code: 200, //请求成功返回
          data: body,
        }
      } else {
        return {
          code: 400, //请求成功返回
          data: body,
          message: '手机号重复，添加失败！',
        }
      }
    },
  },
  {
    /*修改人员信息*/
    url: '/admin/peopleinfo',
    method: 'put',
    response: ({ body }) => {
      console.log('前端传入的修改主体:', body)
      const has = peopleinfo.some((prod: any) => prod.key === body.key)
      // 修改处理逻辑
      if (has) {
        const retain = peopleinfo.filter((item: any) => item.key === body.key)
        let keey: any
        for (keey in body) {
          if (retain[0][keey]) {
            retain[0][keey] = body[keey] ? body[keey] : retain[0][keey]
          } else {
            retain[0][keey] = body[keey]
          }
        }
        console.log('修改完成后的', retain[0])
        // 删除掉原始对象
        const prop = peopleinfo.filter((it: any) => it.key !== body.key)
        // 将俢改后的对象添加进数组
        prop.push(retain[0])
        peopleinfo = prop
        // 写入文件保存
        fs.writeFileSync(__dirname + '/people.json', JSON.stringify(peopleinfo))
        return {
          code: 200, //请求成功返回
          data: peopleinfo,
          message: '修改成功！',
        }
      }
    },
  },
  {
    /*删除人员信息*/
    url: '/admin/peopleinfo',
    method: 'delete',
    response: ({ body }) => {
      console.log('被删除的主体:', body)
      // 处理删除逻辑
      const retain = peopleinfo.filter((item: any) => item.key !== body.key)
      peopleinfo = retain
      // 删除后，重新写回被保留的文件
      fs.writeFileSync(__dirname + '/people.json', JSON.stringify(peopleinfo))
      return {
        code: 200, //请求成功返回
        data: peopleinfo,
      }
    },
  },
  {
    /*批量删除人员信息*/
    url: '/admin/dletepeopleall',
    method: 'delete',
    response: ({ body }) => {
      console.log('被删除的主体（是一个数组）:', body)
      // 处理批量删除逻辑
      body.forEach((it) => {
        // console.log('遍历前端传入的数组', it)
        // console.log('it.key', it.key)

        const index = peopleinfo.findIndex((item) => it.key === item.key)
        // console.log('值相等的索引', index)

        if (index !== -1) {
          peopleinfo.splice(index, 1)
          // console.log(peopleinfo.splice(index, 1))
        }
      })

      // 删除后，重新写回被保留的文件
      fs.writeFileSync(__dirname + '/people.json', JSON.stringify(peopleinfo))
      return {
        code: 200, //请求成功返回
        data: peopleinfo,
        message: '删除成功',
      }
    },
  },

  /********************************************************************************/

  {
    /*获取材料信息+公司信息+库存信息*/
    url: '/admin/materialinfo', // API
    method: 'get',
    response: {
      code: 200, //请求成功返回
      data: materialinfo,
    },
  },
  {
    /*增加材料信息+公司信息+库存信息*/
    url: '/admin/materialinfo',
    method: 'post',
    response: ({ body }) => {
      console.log('被添加的主体:', body)
      // 处理添加逻辑
      const materialNumberbool = materialinfo.some(
        (prod: any) => prod.materialNumber === body.materialNumber,
      )
      const theInvoiceNumberbool = materialinfo.some(
        (prod: any) => prod.theInvoiceNumber === body.theInvoiceNumber,
      )
      const supplierPhonebool = materialinfo.some(
        (prod: any) => prod.supplierPhone === body.supplierPhone,
      )

      if (!materialNumberbool || !theInvoiceNumberbool || !supplierPhonebool) {
        // 如果 材料编码ro手机号or出库单号 没有重复，添加进仓库+写入文件+返回成功的状态
        body['key'] = Math.random()
        materialinfo.push(body)

        // 写入文件保存
        fs.writeFileSync(
          __dirname + '/material.json',
          JSON.stringify(materialinfo),
        )
        return {
          code: 200, //请求成功返回
          data: body,
          message: '添加成功',
        }
      } else {
        return {
          code: 400, //请求成功返回
          data: body,
          message: '材料编码or出库单号重复，添加失败！',
        }
      }
    },
  },
  {
    /*修改材料信息+公司信息+库存信息*/
    url: '/admin/materialinfo',
    method: 'put',
    response: ({ body }) => {
      console.log('前端传入的修改主体:', body)
      const has = materialinfo.some((prod: any) => prod.key === body.key)
      // 修改处理逻辑
      if (has) {
        const retain = materialinfo.filter((item: any) => item.key === body.key)
        let keey: any
        for (keey in body) {
          if (retain[0][keey]) {
            retain[0][keey] = body[keey] ? body[keey] : retain[0][keey]
          } else {
            retain[0][keey] = body[keey]
          }
        }
        console.log('修改完成后的', retain[0])
        // 删除掉原始对象
        const prop = materialinfo.filter((it: any) => it.key !== body.key)
        // 将俢改后的对象添加进数组
        prop.push(retain[0])
        materialinfo = prop
        // 写入文件保存
        fs.writeFileSync(
          __dirname + '/material.json',
          JSON.stringify(materialinfo),
        )
        return {
          code: 200, //请求成功返回
          data: materialinfo,
          message: '修改成功！',
        }
      }
    },
  },
  {
    /*删除材料信息+公司信息+库存信息*/
    url: '/admin/materialinfo',
    method: 'delete',
    response: ({ body }) => {
      console.log('被删除的主体:', body)
      // 处理删除逻辑
      const retain = materialinfo.filter((item: any) => item.key !== body.key)
      materialinfo = retain
      // 删除后，重新写回被保留的文件
      fs.writeFileSync(
        __dirname + '/material.json',
        JSON.stringify(materialinfo),
      )
      return {
        code: 200, //请求成功返回
        data: materialinfo,
        message: '删除成功',
      }
    },
  },
  {
    /*批量删除材料+公司+库存信息*/
    url: '/admin/dletematerialall',
    method: 'delete',
    response: ({ body }) => {
      console.log('被删除的主体（是一个数组）:', body)
      // 处理批量删除逻辑
      body.forEach((it) => {
        // console.log('遍历前端传入的数组', it)
        // console.log('it.key', it.key)

        const index = materialinfo.findIndex((item) => it.key === item.key)
        // console.log('值相等的索引', index)

        if (index !== -1) {
          materialinfo.splice(index, 1)
          // console.log(peopleinfo.splice(index, 1))
        }
      })

      // 删除后，重新写回被保留的文件
      fs.writeFileSync(
        __dirname + '/material.json',
        JSON.stringify(materialinfo),
      )
      return {
        code: 200, //请求成功返回
        data: materialinfo,
        message: '删除成功',
      }
    },
  },
  /**************************************************************************/
  {
    /*获取流程信息*/
    url: '/admin/officeinfo', // API
    method: 'get',
    response: {
      code: 200, //请求成功返回
      data: officeinfo,
    },
  },
  {
    /*增加流程信息*/
    url: '/admin/officeinfo',
    method: 'post',
    response: ({ body }) => {
      // console.log('被添加的主体:', body)

      // 处理添加逻辑
      const has = officeinfo.some(
        (prod: any) => prod.ProcessID === body.ProcessID,
      )
      if (!has) {
        // 如果流程ID没有重复，添加进仓库+写入文件+返回成功的状态
        body['key'] = Math.random()
        officeinfo.push(body)
        // console.log(body)

        // 写入文件保存
        fs.writeFileSync(__dirname + '/office.json', JSON.stringify(officeinfo))
        return {
          code: 200, //请求成功返回
          data: body,
        }
      } else {
        return {
          code: 400, //请求成功返回
          data: body,
          message: '流程ID重复，添加失败！',
        }
      }
    },
  },
  {
    /*修改流程信息*/
    url: '/admin/officeinfo',
    method: 'put',
    response: ({ body }) => {
      console.log('前端传入的修改主体:', body)
      const has = officeinfo.some((prod: any) => prod.key === body.key)
      // 修改处理逻辑
      if (has) {
        const retain = officeinfo.filter((item: any) => item.key === body.key)
        let keey: any
        for (keey in body) {
          if (retain[0][keey]) {
            retain[0][keey] = body[keey] ? body[keey] : retain[0][keey]
          } else {
            retain[0][keey] = body[keey]
          }
        }
        console.log('修改完成后的', retain[0])
        // 删除掉原始对象
        const prop = officeinfo.filter((it: any) => it.key !== body.key)
        // 将俢改后的对象添加进数组
        prop.push(retain[0])
        officeinfo = prop
        // 写入文件保存
        fs.writeFileSync(__dirname + '/office.json', JSON.stringify(officeinfo))
        return {
          code: 200, //请求成功返回
          data: officeinfo,
          message: '修改成功！',
        }
      }
    },
  },
  {
    /*删除流程信息*/
    url: '/admin/officeinfo',
    method: 'delete',
    response: ({ body }) => {
      console.log('被删除的主体:', body)
      // 处理删除逻辑
      const retain = officeinfo.filter((item: any) => item.key !== body.key)
      officeinfo = retain
      // 删除后，重新写回被保留的文件
      fs.writeFileSync(__dirname + '/office.json', JSON.stringify(officeinfo))
      return {
        code: 200, //请求成功返回
        data: officeinfo,
      }
    },
  },
  {
    /*批量删除流程信息*/
    url: '/admin/dleteofficeall',
    method: 'delete',
    response: ({ body }) => {
      console.log('被删除的主体（是一个数组）:', body)
      // 处理批量删除逻辑
      body.forEach((it) => {
        // console.log('遍历前端传入的数组', it)
        // console.log('it.key', it.key)

        const index = officeinfo.findIndex((item) => it.key === item.key)
        // console.log('值相等的索引', index)

        if (index !== -1) {
          officeinfo.splice(index, 1)
          // console.log(peopleinfo.splice(index, 1))
        }
      })

      // 删除后，重新写回被保留的文件
      fs.writeFileSync(__dirname + '/office.json', JSON.stringify(officeinfo))
      return {
        code: 200, //请求成功返回
        data: officeinfo,
        message: '删除成功',
      }
    },
  },

  /**************************************************************************/
  {
    /*获取邮件信息*/
    url: '/admin/mailinfo', // API
    method: 'get',
    response: {
      code: 200, //请求成功返回
      data: mailinfo,
    },
  },
  {
    /*增加邮件信息*/
    url: '/admin/mailinfo',
    method: 'post',
    response: ({ body }) => {
      // console.log('被添加的主体:', body)

      // 处理添加逻辑
      const has = mailinfo.some((prod: any) => prod.key === body.key)
      if (!has) {
        // 如果邮件ID没有重复，添加进仓库+写入文件+返回成功的状态
        body['key'] = Math.random()
        mailinfo.push(body)
        // console.log(body)

        // 写入文件保存
        fs.writeFileSync(__dirname + '/mail.json', JSON.stringify(mailinfo))
        return {
          code: 200, //请求成功返回
          data: body,
        }
      } else {
        return {
          code: 400, //请求成功返回
          data: body,
          message: '邮件ID重复，添加失败！',
        }
      }
    },
  },
  {
    /*修改邮件信息*/
    url: '/admin/mailinfo',
    method: 'put',
    response: ({ body }) => {
      console.log('前端传入的修改主体:', body)
      const has = mailinfo.some((prod: any) => prod.key === body.key)
      // 修改处理逻辑
      if (has) {
        const retain = mailinfo.filter((item: any) => item.key === body.key)
        let keey: any
        for (keey in body) {
          if (retain[0][keey]) {
            retain[0][keey] = body[keey] ? body[keey] : retain[0][keey]
          } else {
            retain[0][keey] = body[keey]
          }
        }
        console.log('修改完成后的', retain[0])
        // 删除掉原始对象
        const prop = mailinfo.filter((it: any) => it.key !== body.key)
        // 将俢改后的对象添加进数组
        prop.push(retain[0])
        mailinfo = prop
        // 写入文件保存
        fs.writeFileSync(__dirname + '/mail.json', JSON.stringify(mailinfo))
        return {
          code: 200, //请求成功返回
          data: mailinfo,
          message: '修改成功！',
        }
      }
    },
  },
  {
    /*删除邮件信息*/
    url: '/admin/mailinfo',
    method: 'delete',
    response: ({ body }) => {
      console.log('被删除的主体:', body)
      // 处理删除逻辑
      const retain = mailinfo.filter((item: any) => item.key !== body.key)
      mailinfo = retain
      // 删除后，重新写回被保留的文件
      fs.writeFileSync(__dirname + '/mail.json', JSON.stringify(mailinfo))
      return {
        code: 200, //请求成功返回
        data: mailinfo,
      }
    },
  },
  {
    /*批量删除邮件信息*/
    url: '/admin/dletemailall',
    method: 'delete',
    response: ({ body }) => {
      console.log('被删除的主体（是一个数组）:', body)
      // 处理批量删除逻辑
      body.forEach((it) => {
        // console.log('遍历前端传入的数组', it)
        // console.log('it.key', it.key)

        const index = mailinfo.findIndex((item) => it.key === item.key)
        // console.log('值相等的索引', index)

        if (index !== -1) {
          mailinfo.splice(index, 1)
          // console.log(peopleinfo.splice(index, 1))
        }
      })

      // 删除后，重新写回被保留的文件
      fs.writeFileSync(__dirname + '/mail.json', JSON.stringify(mailinfo))
      return {
        code: 200, //请求成功返回
        data: mailinfo,
        message: '删除成功',
      }
    },
  },

  /**************************************************************************/
  {
    /*获取计划信息*/
    url: '/admin/planinfo', // API
    method: 'get',
    response: {
      code: 200, //请求成功返回
      data: planinfo,
    },
  },
  {
    /*增加计划信息*/
    url: '/admin/planinfo',
    method: 'post',
    response: ({ body }) => {
      // console.log('被添加的主体:', body)

      // 处理添加逻辑
      const has = planinfo.some((prod: any) => prod.key === body.key)
      if (!has) {
        // 如果计划ID没有重复，添加进仓库+写入文件+返回成功的状态
        body['key'] = Math.random()
        planinfo.push(body)
        // console.log(body)

        // 写入文件保存
        fs.writeFileSync(__dirname + '/plan.json', JSON.stringify(planinfo))
        return {
          code: 200, //请求成功返回
          data: body,
        }
      } else {
        return {
          code: 400, //请求成功返回
          data: body,
          message: '计划ID重复，添加失败！',
        }
      }
    },
  },
  {
    /*修改计划信息*/
    url: '/admin/planinfo',
    method: 'put',
    response: ({ body }) => {
      console.log('前端传入的修改主体:', body)
      const has = planinfo.some((prod: any) => prod.key === body.key)
      // 修改处理逻辑
      if (has) {
        const retain = planinfo.filter((item: any) => item.key === body.key)
        let keey: any
        for (keey in body) {
          if (retain[0][keey]) {
            retain[0][keey] = body[keey] ? body[keey] : retain[0][keey]
          } else {
            retain[0][keey] = body[keey]
          }
        }
        console.log('修改完成后的', retain[0])
        // 删除掉原始对象
        const prop = planinfo.filter((it: any) => it.key !== body.key)
        // 将俢改后的对象添加进数组
        prop.push(retain[0])
        planinfo = prop
        // 写入文件保存
        fs.writeFileSync(__dirname + '/plan.json', JSON.stringify(planinfo))
        return {
          code: 200, //请求成功返回
          data: planinfo,
          message: '修改成功！',
        }
      }
    },
  },
  {
    /*删除计划信息*/
    url: '/admin/planinfo',
    method: 'delete',
    response: ({ body }) => {
      console.log('被删除的主体:', body)
      // 处理删除逻辑
      const retain = planinfo.filter((item: any) => item.key !== body.key)
      planinfo = retain
      // 删除后，重新写回被保留的文件
      fs.writeFileSync(__dirname + '/plan.json', JSON.stringify(planinfo))
      return {
        code: 200, //请求成功返回
        data: planinfo,
      }
    },
  },
  {
    /*批量删除计划信息*/
    url: '/admin/dleteplanall',
    method: 'delete',
    response: ({ body }) => {
      console.log('被删除的主体（是一个数组）:', body)
      // 处理批量删除逻辑
      body.forEach((it) => {
        // console.log('遍历前端传入的数组', it)
        // console.log('it.key', it.key)

        const index = planinfo.findIndex((item) => it.key === item.key)
        // console.log('值相等的索引', index)

        if (index !== -1) {
          planinfo.splice(index, 1)
          // console.log(peopleinfo.splice(index, 1))
        }
      })

      // 删除后，重新写回被保留的文件
      fs.writeFileSync(__dirname + '/plan.json', JSON.stringify(planinfo))
      return {
        code: 200, //请求成功返回
        data: planinfo,
        message: '删除成功',
      }
    },
  },

  /**************************************************************************/
  {
    /*获取计划信息*/
    url: '/admin/scheduleinfo', // API
    method: 'get',
    response: {
      code: 200, //请求成功返回
      data: scheduleinfo,
    },
  },
  {
    /*增加计划信息*/
    url: '/admin/scheduleinfo',
    method: 'post',
    response: ({ body }) => {
      // console.log('被添加的主体:', body)

      // 处理添加逻辑
      const has = scheduleinfo.some((prod: any) => prod.key === body.key)
      if (!has) {
        // 如果计划ID没有重复，添加进仓库+写入文件+返回成功的状态
        body['key'] = Math.random()
        scheduleinfo.push(body)
        // console.log(body)

        // 写入文件保存
        fs.writeFileSync(
          __dirname + '/schedule.json',
          JSON.stringify(scheduleinfo),
        )
        return {
          code: 200, //请求成功返回
          data: body,
        }
      } else {
        return {
          code: 400, //请求成功返回
          data: body,
          message: '计划ID重复，添加失败！',
        }
      }
    },
  },
  {
    /*修改计划信息*/
    url: '/admin/scheduleinfo',
    method: 'put',
    response: ({ body }) => {
      console.log('前端传入的修改主体:', body)
      const has = scheduleinfo.some((prod: any) => prod.key === body.key)
      // 修改处理逻辑
      if (has) {
        const retain = scheduleinfo.filter((item: any) => item.key === body.key)
        let keey: any
        for (keey in body) {
          if (retain[0][keey]) {
            retain[0][keey] = body[keey] ? body[keey] : retain[0][keey]
          } else {
            retain[0][keey] = body[keey]
          }
        }
        console.log('修改完成后的', retain[0])
        // 删除掉原始对象
        const prop = scheduleinfo.filter((it: any) => it.key !== body.key)
        // 将俢改后的对象添加进数组
        prop.push(retain[0])
        scheduleinfo = prop
        // 写入文件保存
        fs.writeFileSync(
          __dirname + '/schedule.json',
          JSON.stringify(scheduleinfo),
        )
        return {
          code: 200, //请求成功返回
          data: scheduleinfo,
          message: '修改成功！',
        }
      }
    },
  },
  {
    /*删除日程信息*/
    url: '/admin/scheduleinfo',
    method: 'delete',
    response: ({ body }) => {
      console.log('被删除的主体:', body)
      // 处理删除逻辑
      const retain = scheduleinfo.filter((item: any) => item.key !== body.key)
      scheduleinfo = retain
      // 删除后，重新写回被保留的文件
      fs.writeFileSync(
        __dirname + '/schedule.json',
        JSON.stringify(scheduleinfo),
      )
      return {
        code: 200, //请求成功返回
        data: scheduleinfo,
      }
    },
  },
  {
    /*批量删除计划信息*/
    url: '/admin/dletescheduleall',
    method: 'delete',
    response: ({ body }) => {
      console.log('被删除的主体（是一个数组）:', body)
      // 处理批量删除逻辑
      body.forEach((it) => {
        // console.log('遍历前端传入的数组', it)
        // console.log('it.key', it.key)

        const index = scheduleinfo.findIndex((item) => it.key === item.key)
        // console.log('值相等的索引', index)

        if (index !== -1) {
          scheduleinfo.splice(index, 1)
          // console.log(peopleinfo.splice(index, 1))
        }
      })

      // 删除后，重新写回被保留的文件
      fs.writeFileSync(
        __dirname + '/schedule.json',
        JSON.stringify(scheduleinfo),
      )
      return {
        code: 200, //请求成功返回
        data: scheduleinfo,
        message: '删除成功',
      }
    },
  },
] as MockMethod[]
